package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.util.List;

/**
 * @FileName: DictController
 * @Author Steven
 * @Date: 2021/3/17
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    DictService dictService;

    @ApiOperation("根据ID查询自数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> childData = dictService.findChildData(id);
        return Result.ok(childData);
    }



    @ApiOperation("数据导出")
    @GetMapping("/exportData")
    public Result exportData(HttpServletResponse response){
        dictService.exportData(response);
        return Result.ok();
    }

    @ApiOperation("导入字典数据")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(
            @ApiParam(name="dictCode",value = "上级编码") @PathVariable String dictCode,
            @ApiParam(name="value",value = "编码的值") @PathVariable String value){
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("", value);
        System.out.println(dictName);
        return dictName;
    }

    @ApiOperation("根据数据ID查询数据列表")
    @GetMapping("/findByDictCode/{desCode}")
    public Result findByDictCode(@PathVariable("desCode") String desCode){
        List<Dict> dictList = dictService.findByDictCode(desCode);
        return Result.ok(dictList);

    }
}
